<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTolls :node-data="companyInfo" :is-company="true" />
        <hr>
        <!-- 这里使用的是作用域插槽，注意：在子组件中，slot没有设置name，默认name为default -->
        <el-tree :data="depts" :default-expand-all="true" :props="{label:'name'}">
          <template #default="{data}">
            <!-- 这里node-data等价于nodeData，这主要是由于eslint导致的 -->
            <TreeTolls
              :node-data="data"
              @isDepartment="isDepartmentFn"
              @isEditorDepartment="isEditorDepartmentFn"
              @deleteDepartment="deleteDepartmentFn"
            />
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 弹出框 -->
    <AddDept ref="addDept" :is-dialog.sync="isDialog" :node-data="nodeData" @addDepartment="getDepartmentList" />
  </div>
</template>

<script>
import TreeTolls from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { getDepartmentList } from '@/api/department'
import { listToTreeData } from '@/utils'
export default {
  components: { TreeTolls, AddDept },
  data() {
    return {
      companyInfo: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人'
      },
      depts: [],
      isDialog: false, // 显示/隐藏弹出框
      nodeData: {}// 获取当前部门的信息
    }
  },
  async created() {
    this.getDepartmentList()
  },
  methods: {
    // 根据id删除部门
    deleteDepartmentFn() {
      // 重新渲染数据
      this.getDepartmentList()
    },
    // 显示/隐藏添加窗口
    isDepartmentFn(nodeData) {
      this.nodeData = nodeData
      // console.log(49, this.nodeData)
      this.isDialog = true
    },
    // 显示/隐藏编辑窗口
    isEditorDepartmentFn(nodeData) {
      this.nodeData = nodeData
      this.isDialog = true
      // console.log(this.nodeData)
      // 父元素获取子元素中的函数,通过ref
      this.$refs.addDept.getDepartment(this.nodeData.id)
    },
    async getDepartmentList() {
      const { depts } = await getDepartmentList()
      this.depts = listToTreeData(depts, '')
    // console.log('获取部门信息', await getDepartmentList())
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
